<template>
    <div class="warpper-box">
        <div class="inner_box">
            <form-create 
            v-model="params.fApi" 
            :rule="params.rule" 
            :option="params.option" 
            @btn-search-click="loadList(true)" 
            @btn-reset-click="reset"/>
            <PurchaseInvitationList
            :key="keys"
            :tableData="tables.tableData" 
            :tableLabel="tables.tableLabel" 
            :models="tables" 
            @on-search-click="loadList"
            @on-enroll-click="function(row){return verifyClick(row,'enroll')}"
            @on-details-click="function(row){return verifyClick(row,'details')}"
            @on-choiceReview-click="function(row){return verifyClick(row,'choiceReview')}"
            @on-review-click="function(row){return verifyClick(row,'review')}"
            @on-bidVerify-click="function(row){return verifyClick(row,'bidReview')}""
            @on-cancelEnroll-click="cancelEnroll"
            @on-publicity-click="publicity"
            @on-bidCancel-click="bidCancel"
            @on-invitationFailBid-click="invitationFailBid"
            @on-del-click="del"
            @on-stop-click="stopInvitation"/>

            <el-dialog :title="verify.enroll.title" :visible.sync="verify.enroll.visible" :width="verify.width" :close-on-click-modal="false">
              <invitationSupplierEnroll v-if="verify.enroll.hackReset" :row="verify.row"  @on-cancel-click="onCancel"/>
            </el-dialog>
            <!--选专家-->
            <el-dialog :title="verify.choiceReview.title" :visible.sync="verify.choiceReview.visible" :width="verify.width" :showClose="false" :close-on-click-modal="false">
              <invitationChoice v-if="verify.choiceReview.hackReset" :row="verify.row"  @on-cancel-click="onCancel"/>
            </el-dialog>
            <!--打分-->
            <el-dialog :title="verify.review.title" :visible.sync="verify.review.visible" :width="verify.width" :showClose="false" :close-on-click-modal="false">
              <invitationReview v-if="verify.review.hackReset" :row="verify.row"  @on-cancel-click="onCancel"/>
            </el-dialog>
            <!---中标审核-->
            <el-dialog :title="verify.bidReview.title" :visible.sync="verify.bidReview.visible" :width="verify.width" :showClose="false" :close-on-click-modal="false">
              <invitationBidReview v-if="verify.bidReview.hackReset" :row="verify.row"  @on-cancel-click="onCancel"/>
            </el-dialog>

            <el-dialog :title="verify.details.title" :visible.sync="verify.details.visible" :width="verify.width" :showClose="false" :close-on-click-modal="false">
              <invitationDetails v-if="verify.details.hackReset" :row="verify.row"  @on-cancel-click="onCancel"/>
            </el-dialog>


        </div>
    </div>
  </template>
  
  <script>
  import moment from 'moment'
  import { 
    PARAMS_PURCHASE_INQUIRY,
    LABEL_PURCHASE_INVITATION,
    LABEL_PURCHASE_INVITATION_SUPPLIER,
    OPTION_INLINE
  } from "@/views/purchase/purchaseModel/purchase_form"
  import PurchaseInvitationList from "@/views/purchase/purchaseModel/purchase_invitation_model_list"
  import invitationSupplierEnroll from "@/views/purchase/purchaseInvitation/invitation_supplier_enroll"
  import invitationReview from "@/views/purchase/purchaseInvitation/invitation_review"
  import invitationChoice from "@/views/purchase/purchaseInvitation/invitation_choice"
  import invitationBidReview from "@/views/purchase/purchaseInvitation/invitation_bid_review"
  import invitationDetails from "@/views/purchase/purchaseInvitation/invitation_details"
  import { 
    bidPurchaseCommonList,
    bidPurchaseSupplierCancelQuote,
    bidPurchaseOvert,
    bidPurchaseStop,
    bidPurchaseDel,
    bidPurchaseInquiryInvitationCancel
  } from "@/api/purchase_api"
  export default {
    components:{
      PurchaseInvitationList,
      invitationSupplierEnroll,
      invitationReview,
      invitationChoice,
      invitationBidReview,
      invitationDetails
    },
    data() {
      return {
        keys:0,
        params:{
          fApi:{},
          rule:PARAMS_PURCHASE_INQUIRY,
          option:OPTION_INLINE
        },
        tables:{
          total:0,
          fixed:'right',
          width:'300px',
          tableData:[],
          tableLabel: LABEL_PURCHASE_INVITATION
        },
        page:{
          purchaseType:'2',
          pageNum:1,
          pageSize:10,
        },
        verify:{
          row:{},
          width:"1300px",
          enroll:{
            title:'供应商报名',
            visible:false,
            hackReset:true
          },
          choiceReview:{
            title:'分配处理人',
            visible:false,
            hackReset:true
          },
          review:{
            title:'邀请招标评审',
            visible:false,
            hackReset:true
          },
          bidReview:{
            title:'邀请招标中标审核',
            visible:false,
            hackReset:true
          },
          details:{
            title:'邀请招标详情',
            visible:false,
            hackReset:true
          }
        },
        expertName:'',//专家名称
        openTime:'',//开标时间
        pickerOptions: {
            shortcuts: [{
                text: '今天',
                onClick(picker) {
                picker.$emit('pick', new Date());
                }
            }, {
                text: '昨天',
                onClick(picker) {
                const date = new Date();
                date.setTime(date.getTime() - 3600 * 1000 * 24);
                picker.$emit('pick', date);
                }
            }, {
                text: '一周前',
                onClick(picker) {
                const date = new Date();
                date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit('pick', date);
                }
            }]
          }
      };
    },
    mounted() {
      this.loadTableLalbe();
      this.$nextTick(()=>{
        this.params.fApi.setValue({"supplierName":"","name":"","time":""});
      })
    },
    methods: {
      loadTableLalbe(){
        let user = this.$store.getters.permission_user;
        if(user.purchaseSupplier !== null){//供应商角色
          this.tables.tableLabel = LABEL_PURCHASE_INVITATION_SUPPLIER;// 列表字段动态
          this.params.fApi.hidden(true,'supplierName');//搜索条件隐藏
        }else{//其他角色
          this.tables.tableLabel = LABEL_PURCHASE_INVITATION;// 列表字段动态
          this.params.fApi.hidden(false,'supplierName');//搜索条件隐藏
        }
        this.loadList(true);
      },
      async loadList(formData) {
        let params = this.params.fApi.bind();
        if(params.time && params.time.length != 0){
          params.startTime = params.time[0];
          params.endTime = params.time[1];
        }
        if(formData !== undefined && formData.type === 'page'){
          this.page.pageNum = formData.pageNum;
          this.page.pageSize = formData.pageSize;
        }
        if(formData===true){
          this.page.pageNum = 1;
          this.keys++;
        }
        params.projectType = 1;//1物资，2劳务
        params.bidType = this.page.purchaseType;
        params.pageNum = this.page.pageNum;
        params.pageSize = this.page.pageSize;
        const req = await bidPurchaseCommonList(params);
        if(req.code === 200){
          req.data.list.forEach(v=>{
            v.winningMoney = (v.winningMoney/100);
          })
          this.tables.tableData = req.data.list;
          let thisTime = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");//当前报名时间
          let thisDateTime = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");//当前开标时间  
          let btn = this.$store.getters.permission_btns;
          let user = this.$store.getters.permission_user;
          let _supplierId = user.purchaseSupplier === null ? '' : String(user.purchaseSupplier.id);//不为空就是供应商
           //1.供应商报名 1.取消报名 1抽取专家,2.专家评审 3.中标评审 4.中标公示 6.终止
          this.tables.tableData.forEach(v=>{
            v.showFlag = v.status;
            if(v.status === "1"){
              const flag = thisTime >= moment(v.startTime).format("YYYY-MM-DD HH:mm:ss") && thisTime <= moment(v.endTime).format("YYYY-MM-DD HH:mm:ss");
              //供应商角色
              if(user.purchaseSupplier !== null && flag){
                let _supplier = v.supplierIds.split(',');
                if(_supplier.includes(_supplierId)){ //列表包含当前账户 存在就取消报名
                  v.cancelEnroll = btn.includes('invitationBidSupplierCancelEnroll')//是否有权限
                }else{//不存在 就报价
                    v.enroll = btn.includes('invitationBidSupplierEnroll')//是否有权限
                }
              }

              //抽取专家
              if(thisTime > moment(v.endTime).format("YYYY-MM-DD HH:mm:ss") && !v.expertIds){//已经结束报名 并且没有抽取专家
                  v.choiceReview = btn.includes('invitationBidExtractHandler')//是否有权限
                  v.showFlag = '9';//待选专家
              }


              if(v.expertIds && thisDateTime < moment(v.openTime).format("YYYY-MM-DD HH:mm:ss")){
                  v.showFlag = '8';//已经选专家 待开标
              }
              v.showFlag = v.showFlag;
            }

            //开标时间到了以后 专家评标
            if(v.status === '2'){
              v.review = btn.includes('invitationBidReview');
            }

             //评分完成 中标审核
            if(v.status === '3'){
              v.bidVerify = btn.includes('invitationBidVerify');
            }

            //标的已公示
            if(v.status === '4'){
              if(v.gsFlag === '2' || v.gsFlag === '0'){//撤回的 未公示的 都可以继续公示
                v.publicity = btn.includes('invitationBidBiddingPublicity');
              }else if(v.gsFlag === '1'){//已公示的继续撤回
                v.bidCancel = btn.includes('invitationBidCancel');
              }
            }

            if(v.status === '6'){
               //终止
              v.stop = btn.includes('invitationBidStopPurchase');
               //删除
              //v.del = btn.includes('invitationBidDel');//是否有权限
            }

            //中标审核完成 中标公示
            if(v.status === '7'){
              v.publicity = btn.includes('invitationBidBiddingPublicity');
            }

            if(v.status !== '5'){//不是流标状态
              v.invitationFail = btn.includes('invitationFailCode'); //流标
            }
            v.details = true;
          })
          this.tables.total = req.data.total;
        }
      },
      //终止项目
      stopInvitation(row){
        this.$confirm("终止后无法恢复，是否终止", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(async () => {
           const {code} = await bidPurchaseStop(row.id);
           if(code === 200){
            this.$message.success("操作成功");
            this.loadList();
           }
        }).catch(() => {});
      },
      //流标
      invitationFailBid(row){
        this.$confirm("流标后无法恢复，是否流标", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(async () => {
          let params = {};
          params.explain = '';
          params.id = row.id;//标id
          const {code} = await bidPurchaseInquiryInvitationCancel(params);
           if(code === 200){
            this.$message.success("操作成功");
            this.loadList();
           }
        }).catch(() => {});
        /* this.$prompt('流标后无法恢复，是否流标', '温馨提示', {
          confirmButtonText: '流标',
          cancelButtonText: '取消',
        }).then(async({ value }) => {
          let params = {};
          params.explain = value;
          params.id = row.id;//标id
          const {code} = await bidPurchaseInquiryInvitationCancel(params);
          if(code === 200){
            this.$message.success("操作成功");
            this.loadList();
          }
        }).catch(() => {}); */
      },
      //取消报价
      cancelEnroll(row){
        this.$confirm('确定取消报价', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(async () => {
            const { code } = await bidPurchaseSupplierCancelQuote(row.id);
            if(code === 200){
              this.$message.success("操作成功");
              this.loadList();
            }
        }).catch(() => {});
      },
      //中标公示
      publicity(row){
        this.$confirm('确定中标公示', "提示", {
          confirmButtonText: "公示",
          cancelButtonText: "不公示",
          distinguishCancelAndClose: true,
          type: "warning"
        }).then(async () => {
          //1 公示，0 不公示
            const { code } = await bidPurchaseOvert(row.id,1) ;
            if(code === 200){
              this.$message.success("操作成功");
              this.loadList();
            }
        }).catch(async (action) => {
          if(action === 'cancel'){
            const { code } = await bidPurchaseOvert(row.id,0) ;
            if(code === 200){
              this.$message.success("操作成功");
              this.loadList();
            }
          }
        });
      },
      //撤回中标公示
      bidCancel(row){
        this.$confirm('确定撤回公示的标', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(async () => {
          //1 公示，0 不公示 2撤回
            const { code } = await bidPurchaseOvert(row.id,2) ;
            if(code === 200){
              this.$message.success("操作成功");
              this.loadList();
            }
        }).catch(() => {});
      },
      //新增和报名
      verifyClick(row,typeVisible){
        this.verify.row = row;
        this.verify[typeVisible].hackReset = false;
        this.$nextTick(()=>{
          this.verify[typeVisible].hackReset = true;
        })
        this.verify[typeVisible].visible = true;
      },
      async del(row){
        await bidPurchaseDel(row.id)
      },
      reset(){
        this.params.fApi.resetFields();
        this.loadList(true);
      },
      onCancel(flag){
        this.verify[flag].visible = false;
        this.loadList(true);
      }
    }
  };
  </script>